package com.wenjie.springbootbugdetection.web;

import com.wenjie.springbootbugdetection.model.Admin;
import com.wenjie.springbootbugdetection.model.Development;
import com.wenjie.springbootbugdetection.model.Submitter;
import com.wenjie.springbootbugdetection.model.Test_manager;
import com.wenjie.springbootbugdetection.service.AdminService;
import com.wenjie.springbootbugdetection.service.DevelopmentService;
import com.wenjie.springbootbugdetection.service.SubmitterService;
import com.wenjie.springbootbugdetection.service.Test_managerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
public class LoginController {
    @Autowired  //与service层进行交互
    private SubmitterService submitterService;
    @Autowired
    private Test_managerService test_managerService;
    @Autowired
    private DevelopmentService developmentService;
    @Autowired
    private AdminService adminService;

    /**
     *
     * @param id
     * @param password
     * @return
     */
    @RequestMapping("/user")
    public String getLogin(HttpServletRequest request, @RequestParam("id") String id, @RequestParam("password") String password){
        Integer ID = null;
        boolean flag = true;

        if (id.length() >= 11){
            flag = false;
        }else{
            ID = Integer.valueOf(id);
        }
        if (id == null || password == null){
            flag = false;
        }
        if (flag == false){
            return "wrong";
        }

        List<Admin> adminList = adminService.select_All_Admins();
        for (int i = 0; i < adminList.size(); i++) {
            if (adminList.get(i).getAdminId().intValue() == ID){
                if (adminList.get(i).getAdminPassword().equals(password)){
                    request.getSession().setAttribute("name",adminList.get(i).getAdminName());
                    request.getSession().setAttribute("id",adminList.get(i).getAdminId());
                    return "redirect:/admin";
                }
            }
        }

        List<Submitter> submitterList = submitterService.select_All_Submitters();
        for (int i = 0; i < submitterList.size(); i++) {
            if (submitterList.get(i).getSubmitterId().intValue() == ID){
                if (submitterList.get(i).getSubmitterPassword().equals(password)){
                    request.getSession().setAttribute("name",submitterList.get(i).getSubmitterName());
                    request.getSession().setAttribute("id",submitterList.get(i).getSubmitterId());
                    return "redirect:/findAllBugs_submitter";
                }
            }
        }

        List<Test_manager> test_managerList = test_managerService.select_All_Managers();
        for (int i = 0; i < test_managerList.size(); i++) {
            if (test_managerList.get(i).getTestManagerId().intValue() == ID){
                if (test_managerList.get(i).getTestPassword().equals(password)){
                    request.getSession().setAttribute("name",test_managerList.get(i).getTestManagerName());
                    request.getSession().setAttribute("id",test_managerList.get(i).getTestManagerId());
                    return "redirect:/findAllBugs_test_manager";
                }
            }
        }

        List<Development> developmentList = developmentService.select_All_Development();
        for (int i = 0; i < developmentList.size(); i++) {
            if (developmentList.get(i).getDevelopmentId().intValue() == ID){
                if (developmentList.get(i).getDevelopmentPassword().equals(password)){
                    request.getSession().setAttribute("name",developmentList.get(i).getDevelopmentName());
                    request.getSession().setAttribute("id",developmentList.get(i).getDevelopmentId());
                    return "redirect:/findAllBugs_development";
                }
            }
        }
        return "wrong";
    }
}